import requests
# import random
import chatgpt
'下面这个函数用来判断信息开头的几个字是否为关键词'
'如果是关键词则触发对应功能，群号默认为空'


def keyword(message, uid, gid=None):
    # if message[0:3] == '300':  # 300查团分, 格式为300+游戏名称，如 “300yaq”
    #     return zhanji(uid, gid, message[3:len(message)])
    if message[0:4] == 'setu':  # 你们懂的
        setu(gid)
    if message[0:4] == 'chat':
        chat(gid, message)


# def zhanji(uid, gid, name):
#     '本功能参考300英雄官方api文档写成'
#     '有不理解的地方可以看看https://300report.jumpw.com/static/doc/openapi.txt'

#     url = 'https://300report.jumpw.com/api/getrole?name=' + name
#     menu = requests.get(url)
#     for i in menu.json()['Rank']:
#         if i['RankName'] == '团队实力排行':
#             tuanfen = i['Value']
#     if gid != None:  # 如果是群聊信息
#         requests.get(
#             url=
#             'http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}团分{2}'
#             .format(gid, name, tuanfen))
#     else:  # 如果是私聊信息
#         requests.get(
#             url=
#             'http://127.0.0.1:5700/send_private_msg?user_id={0}&message={1}团分{2}'
#             .format(uid, name, tuanfen))


def setu(gid):
    '''本功能放在下面讲，这里的功能默认只有群聊，没考虑私聊，请把机器人拉进群再发消息'''
    '''如果想实现私聊功能可以参考上面查战绩的代码'''
    # 反馈收到信息
    msg = "涩涩请求已收到！正在寻找涩图"
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, msg))

    # 调用api并获取涩图
    # url_num = random.randint(1, 100) % 3
    url_num = 0
    urls = [
        'https://api.lolicon.app/setu/v2',
        # 'https://api.vvhan.com/api/mobil.girl?type=json',
        # 'https://api.vvhan.com/api/girl?type=json'
    ]
    url = urls[url_num]
    menu = requests.get(url).json()
    # print(url_num)
    if url_num == 0:
        setu_url = menu['data'][0]['urls']['original']  # 对传回来的涩图网址进行数据提取
    elif url_num == 1 or 2:
        setu_url = menu['imgurl']
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, r'[CQ:image,'
               r'file=' + str(setu_url) + r']'))

    # 反馈涩图请求结束
    msg = "涩图已出，没有就是出bug了"
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, msg))


def chat(gid, message):
    msg = "chat请求已收到！正在思考问题"
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, msg))

    msg = message[5:]
    respondmsg = chatgpt.askChatGPT(msg)
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, respondmsg))

    msg = "问题已回复，没有就是出bug了"
    requests.get(
        url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.
        format(gid, msg))
